using System;
using System.Text;
using Sharpmark.PerfectLife.ColorfulPhotos.Framework.Picture;

namespace Sharpmark.PerfectLife.ColorfulPhotos.Framework.Utilities.ImageToText
{
	public class TextPhoto : TextBase
	{
		protected Photo _photo;			// 原始图像
		protected bool _isAntiColor;
		protected int _grayLevel;
		protected char[] _replaceChars;	// 准备替换的字符数组

		public bool IsAntiColor
		{
			get { return _isAntiColor; }
			set { _isAntiColor = value; }
		}

		public int GrayLevel
		{
			get { return _grayLevel; }
			set
			{
				_grayLevel = value;
				_replaceChars = (new ReplaceCharsList())[_grayLevel];
			}
		}

		public char[] ReplaceChars
		{
			get { return _replaceChars; }
			set
			{
				_replaceChars = value;
				_grayLevel = _replaceChars.Length;
			}
		}

		public Photo Photo
		{
			get { return _photo; }
			set { _photo = value; }
		}

		public TextPhoto(Photo photo, int charsWidth, int charsHeight, char[] replaceChars)
			: base(charsWidth, charsHeight)
		{
			ReplaceChars = replaceChars;
			IsAntiColor =  false;
			_photo = photo;
		}

		public TextPhoto(Photo photo, int charsWidth, int charsHeight, int replaceCharIndex)
			: base(charsWidth, charsHeight)
		{
			GrayLevel = replaceCharIndex;
			_isAntiColor = false;
			_photo = photo;
		}

		public TextPhoto(Photo photo, int charsWidth, int charsHeight)
			: this(photo, charsWidth, charsHeight, ReplaceCharsList.Chars94)
		{
		}

		public TextPhoto(Photo photo)
			: this(photo, photo.Width, photo.Height)
		{
		}

		public TextPhoto(System.Drawing.Bitmap image)
			: this(new Photo(image))
		{
		}

		public override string GenerateText()
		{
			StringBuilder sb = new StringBuilder();

			Photo thumbnail = _photo.GetThumbnailImage(_charsWidth, _charsHeight);

			for (int h = 0; h < _charsHeight; h++)
			{
				for (int w = 0; w < _charsWidth; w++)
				{
					System.Drawing.Color cc = thumbnail.GetPixel(w, h);
					int sum = (cc.R + cc.G + cc.B) / 3;
					if (IsAntiColor) sum = 255 - sum;
					int ci = sum * ReplaceChars.Length / 256;
					sb.Append(ReplaceChars[ci]);
				}

				sb.Append(System.Environment.NewLine);
			}			
			return sb.ToString();
		}
		/* Code no use.
		 * 
					// method 1 in GenerateText()
					int wpc = _photo.Width / _charsWidth;		// width per char
					int hpc = _photo.Height / _charsHeight;		// height per char

					for (int h = 0; h < height; h++) {
						for (int w = 0; w < width; w++) {
							sb.Append(GetCharFromImageBlock(w, h, wp, hp, replaceChars));
						}
						sb.Append(System.Environment.NewLine);
					}


				private char GetCharFromImageBlock(int width, int height, int wp, int hp, char[] replaceChars)
				{
					int sum = 0;
					int ws = width * wp;
					int hs = height * hp;
					try
					{
						for (int w = ws; w < ws + wp; w++)
						{
							for (int h = hs; h < hs + hp; h++)
							{
								// if (a >= _image.Height || b >= _image.Width) break;
								System.Drawing.Color cc = _photo.GetPixel(w, h);

								sum += (cc.R + cc.G + cc.B) / 3;
							}
						}
					}
					catch (Exception e) { }

					sum /= wp * hp;

					return replaceChars[(255 - sum) * replaceChars.Length / 256];
				}
		*/
	}
}
